//
//  JingxuanCell.m
//  HomeIn
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "JingxuanCell.h"
#import "FirstPageModel.h"

@implementation JingxuanCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self creataSubviews];
    }
    return  self;
}

- (void)creataSubviews
{
    self.imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
    //self.imageView.backgroundColor = [UIColor orangeColor];
    [self.contentView addSubview:self.imageView];
    
    //时间
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.imageView.frame.size.width / 2, self.imageView.frame.size.height / 5)];
    [self.imageView addSubview:self.timeLabel];

    
    //标题阴影 (渐变)
    UIView *titltView = [[UIView alloc] initWithFrame:CGRectMake(0, (self.imageView.frame.size.height / 5) * 4, self.imageView.frame.size.width, self.imageView.frame.size.height / 5)];
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = titltView.frame;
    layer.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor, (id)[UIColor lightGrayColor].CGColor, nil];
    for (CALayer *sublayer in [titltView.layer sublayers]) {
        [sublayer removeFromSuperlayer];
    }
    [titltView.layer insertSublayer:layer atIndex:0];
    titltView.frame = layer.bounds;
    [self.imageView addSubview:titltView];
    
    //标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, (self.imageView.frame.size.height / 5) * 4, self.imageView.frame.size.width, self.imageView.frame.size.height / 5)];
    [self.imageView addSubview:self.titleLabel];
    
    //喜欢阴影
    UIView *likeView = [[UIView alloc] initWithFrame:CGRectMake((self.imageView.frame.size.width / 4.5) * 3.5 - 10, 10, self.imageView.frame.size.width / 4.5, self.imageView.frame.size.height / 5)];
    likeView.backgroundColor = [UIColor blackColor];
    likeView.alpha = 0.3;
    likeView.layer.masksToBounds = YES;//masksToBounds裁剪掉边框
    likeView.layer.cornerRadius = 15;//设置圆角度
    [self.imageView addSubview:likeView];
    
    //爱心
    self.heartImageView = [[UIImageView alloc] initWithFrame:CGRectMake(likeView.frame.origin.x + 5, likeView.frame.origin.y + 5, self.imageView.frame.size.height / 6, self.imageView.frame.size.height / 6)];
    self.heartImageView.image = [[UIImage imageNamed:@"big_heart_grey@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消渲染
    [self.imageView addSubview:self.heartImageView];
    
    //喜欢
    self.likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.heartImageView.frame.origin.x + self.heartImageView.frame.size.width , likeView.frame.origin.y, likeView.frame.size.width / 3 * 2, self.imageView.frame.size.height / 5)];
    [self.imageView addSubview:self.likeLabel];

}

- (void)setFirstPageModel:(FirstPageModel *)firstPageModel
{
    _firstPageModel = firstPageModel;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:_firstPageModel.cover_image_url]];
    
    self.titleLabel.text = _firstPageModel.title;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont systemFontOfSize:19];
    
    self.likeLabel.text = [NSString stringWithFormat:@"%ld",_firstPageModel.likes_count];
    self.likeLabel.layer.masksToBounds = YES;//masksToBounds裁剪掉button的边框
    self.likeLabel.layer.cornerRadius = 15;//设置圆角度
    self.likeLabel.textColor = [UIColor whiteColor];
    self.likeLabel.textAlignment = NSTextAlignmentCenter;
    self.likeLabel.font = [UIFont systemFontOfSize:15];
    
    //时间戳转化时间(10位时间戳)
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:_firstPageModel.created_at];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    self.timeLabel.text = confromTimespStr;
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:17];
}


@end
